package com.bweab.dao.impl;

import com.bweab.dao.api.UserTbDaoInterface;
import com.bweab.dao.base.BaseDao;
import com.bweab.pojo.UsersTb;
import com.bweab.pojo.en.UserDepositDormitoryEn;

import java.util.List;

/**
 * 用户信息查询DAO
 * */
public class UsersTbDaoImpl extends BaseDao<UsersTb> implements UserTbDaoInterface {

    /**
     * 用户信息查询
     * @param username 用户账号
     * 根据用户名查找信息
     * */
    @Override
    public UsersTb getUserInfo(String username) {
        String sql="select\n" +
                "    user_id userId,\n" +
                "    user_name userName,\n" +
                "    user_password userPassword,\n" +
                "    user_sex userSex,\n" +
                "    user_xh userXh,\n" +
                "    user_phone userPhone,\n" +
                "    user_status userStatus\n" +
                "from\n" +
                "    users_tb\n" +
                "where\n" +
                "    user_name=?";
        UsersTb user = querySingle(sql, UsersTb.class, username);

        return user;
    }
/**
 * @param username 用户姓名
 * @param userpassword 用户密码
 * @param userSex //用户性别
 * @param userXh //用户学号
 * @param userPhone //用户手机号
 * 该方法用于插入用户表   注册
 *
 * */
    @Override
    public int insertUser(String username, String userpassword, String userSex, String userXh, String userPhone) {
        //准备sql语句
        //插入用户信息
        String sql="insert into users_tb values(\n" +
                "                            default,\n" +
                "                            ?,\n" +
                "                            ?,\n" +
                "                            ?,\n" +
                "                            ?,\n" +
                "                            ?,\n" +
                "                            default\n" +
                "                           )";
        //调用插入方法
        int rows = alter(sql, username, userpassword, userSex, userXh, userPhone);


        return rows;
    }

    @Override
    public int alterUserInfo(String userName, String userSex, String userPhone, String userXh) {
        //根据用户名修改用户状态
        String sql= "update  users_tb set\n" +
                "                     user_sex=?,\n" +
                "                     user_xh=?,\n" +
                "                     user_phone=?\n" +
                "     where user_name=?";
        //修改
        int rows = alter(sql,userSex,userXh,userPhone,userName);

        return rows;
    }

    /**
     * 用于修改用户密码
     * @param username 用户名称
     * @param userpassword 用户密码
     *
     *
     * @return
     * 修改用户密码
     */
    @Override
    public int alterUserPwd(String username, String userpassword) {
        //准备sql
        String sql= "update  users_tb set\n" +
                "                     user_password=?\n" +
                "     where user_name=?";
        int row = alter(sql,userpassword,username);

        return row;
    }

    @Override
    public int adminAddUser(String username, String userPhone, String userStatus, String userXh, String userSex, String userpassword) {
        //添加sql语句
        String sql="insert into users_tb values(\n" +
                "                            default,\n" +
                "                            ?,\n" +
                "                            ?,\n" +
                "                            ?,\n" +
                "                            ?,\n" +
                "                            ?,\n" +
                "                            ?\n" +
                "                           )";
        //调用插入方法
        int rows = alter(sql, username, userpassword, userSex, userXh, userPhone,userStatus);


        return rows;
    }

    @Override
    public int adminAlterUserInfo(String userName, String userPassword, String userSex, String userXh, String userStatus, String userPhone) {
        //准备管理员修改用户信息的sql语句
        String sql="update users_tb set\n" +

                "    user_name=?,\n" +
                "    user_password=?,\n" +
                "    user_sex=?,\n" +
                "    user_xh=?,\n" +
                "    user_phone=?,\n" +
                "    user_status=?\n" +

                "where\n" +
                "    user_name=?";

        int rows = alter(sql,userName,userPassword,userSex,userXh,userPhone,userStatus,userName);
        return rows;
    }

    /**
     * 通过用户id删除用户
     * @param userId 用户id
     * @return
     */
    @Override
    public int adminDeleteUserInfoByUserId(int userId) {
        //准备删除用的sql

        String sql="delete from users_tb\n" +
                "where user_id=?";

        int rows = alter(sql, userId);
        return rows;
    }

    /**
     * 通过用户名删除用户
     *
     */

    public int adminDeleteUserInfoByUserName(String userName) {
        //准备sql
        String sql="delete from users_tb\n" +
                "where user_name=?";

        int rows = alter(sql, userName);
        return rows;


    }




}
